
package net.atremmedia.gwt.actionjava.examples.metrics.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.geom.Color;
import actionjava.geom.Point2D;
import actionjava.geom.curve.ArcCurve2D;
import actionjava.geom.curve.ArcTanCurve2D;
import actionjava.geom.curve.CubicCurve2D;
import actionjava.geom.curve.QuadCurve2D;
import actionjava.geom.shape.Rectangle2D;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;

public class MetricsExample1 extends AJExample
{
	public static final String NAME = "MetricsExample 1";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-basics/src/main/java/net/atremmedia/gwt/actionjava/examples/metrics/samples/MetricsExample1.java";

	private Shape curveShape = new Shape();

	public MetricsExample1(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		Graphics graph = curveShape.getGraphics();
		// drawArc(graph);
		// drawArcTo(graph);
		drawQuadCurve(graph);
		// drawCubicCurve(graph);

		curveShape.setX(100);
		curveShape.setY(100);
		addChild(curveShape);
	}

	public void drawArc(Graphics graph)
	{
		Point2D p1 = new Point2D(200, 200);

		double startAngle = -90;
		double extentAngle = -210;
		double endAngle = extentAngle + startAngle;

		int radius = 50;

		// Arc curve bounds (Rectangle)
		// IRectangle2D bounds = new ArcCurve2D(p1.getX(), p1.getY(), radius, startAngle, extentAngle).getBounds();
		// graph.beginStroke("CCCCCC");
		// graph.drawRect(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight());
		// graph.endStroke();

		ArcCurve2D arcCurve2D = new ArcCurve2D(p1.x, p1.y, radius, Math.toRadians(startAngle), Math.toRadians(endAngle), ((extentAngle < 0) ? true : false));
		// Start point
		Point2D p2 = arcCurve2D.getArcStartPoint();
		// End point
		Point2D p3 = arcCurve2D.getArcEndPoint();

		// P1 dot
		graph.beginStroke("#FFFF00").beginFill("#FFFF00").drawCircle(p2.x, p2.y, 5).endStroke().endFill();
		// P2 dot
		graph.beginStroke("#FF0000").beginFill("#00FF00").drawCircle(p3.x, p3.y, 5).endStroke().endFill();

		// Arc curve line
		graph.beginStroke("#FF0000");
		// graph.moveTo(500, 600);
		GWT.log("TESTVIEW: startAngle: " + startAngle + " endAngle: " + endAngle + " extentAngle: " + extentAngle + " radius: " + radius + " anticlockwise: " + ((extentAngle < 0) ? true : false));
		graph.arc(p1.x, p1.y, radius, Math.toRadians(startAngle), Math.toRadians(endAngle), (extentAngle < 0) ? true : false);
		graph.endStroke();
	}

	public void drawArcTo(Graphics graph)
	{
		// Start point
		Point2D p1 = new Point2D(200, 0);
		// Control point 1
		Point2D cp1 = new Point2D(450, 0);
		// Control point 2
		Point2D cp2 = new Point2D(355, 200);
		// Radius
		double radius = 100;

		// ArcTo curve bounds (Rectangle)
		ArcTanCurve2D arcTo = new ArcTanCurve2D(p1, cp1, cp2, radius);
		// IRectangle2D bounds = arcTo.getBounds();

		// graph.beginStroke("CCCCCC");
		// graph.drawRect(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight());
		// graph.endStroke();

		// graph.beginFill(Graphics.getRGBA(0xFF9900, .25));
		// graph.drawCircle(arcTo.getX(), arcTo.getY(), radius);
		// graph.endFill();

		// P1 dot
		// graph.beginStroke("#000000").beginFill("#000000").drawCircle(p1.getX(), p1.getY(), 5).endStroke().endFill();
		// CP1 dot
		// graph.beginStroke("#00FF00").beginFill("#00FF00").drawCircle(cp1.getX(), cp1.getY(), 5).endStroke().endFill();
		// CP2 dot
		// graph.beginStroke("#00FF00").beginFill("#00FF00").drawCircle(cp2.getX(), cp2.getY(), 5).endStroke().endFill();
		// CENTER dot
		// graph.beginStroke("#000000").beginFill("#000000").drawCircle(arcTo.getX(), arcTo.getY(), 5).endStroke().endFill();
		// P3 dot
		// graph.beginStroke("#000000").beginFill("#000000").drawCircle(p3.getX(), p3.getY(), 5).endStroke().endFill();
		// Start point
		graph.beginFill(Color.getRGBA(0xFF0000, .25)).drawCircle(arcTo.getArcStartPoint().x, arcTo.getArcStartPoint().y, 5).endFill();
		// End point
		graph.beginFill(Color.getRGBA(0X40cce1, .25)).drawCircle(arcTo.getArcEndPoint().x, arcTo.getArcEndPoint().y, 5).endFill();

		Log.info("PLEAAAAAAASE!!!");

		// ArcTo curve line
		graph.moveTo(p1.x, p1.y);
		graph.beginStroke("#FF0000");
		graph.arcTo(cp1.x, cp1.y, cp2.x, cp2.y, radius);
		graph.endStroke();
	}

	public void drawQuadCurve(Graphics graph)
	{
		// Start point
		Point2D p1 = new Point2D(100, 100);
		// Control point
		Point2D cp1 = new Point2D(50, 600);
		// End point
		Point2D p2 = new Point2D(500, 400);

		// Quadratic curve bounds (Rectangle)
		Rectangle2D bounds = new QuadCurve2D(p1, cp1, p2).getBounds();
		graph.beginStroke("CCCCCC");
		graph.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
		graph.endStroke();

		// P1 dot
		graph.beginStroke("#000000").beginFill("#000000").drawCircle(p1.x, p1.y, 5).endStroke().endFill();
		// CP1 dot
		graph.beginStroke("#00FF00").beginFill("#00FF00").drawCircle(cp1.x, cp1.y, 5).endStroke().endFill();
		// P2 dot
		graph.beginStroke("#000000").beginFill("#000000").drawCircle(p2.x, p2.y, 5).endStroke().endFill();

		// Line connecting P1 / CP1 / P2
		graph.beginStroke("#00FF00");
		graph.moveTo(p1.x, p1.y);
		graph.lineTo(cp1.x, cp1.y);
		graph.lineTo(p2.x, p2.y);
		graph.endStroke();

		// Quadratic curve line
		graph.beginStroke("#FF0000");
		graph.moveTo(p1.x, p1.y);
		graph.quadraticCurveTo(cp1.x, cp1.y, p2.x, p2.y);
		graph.endStroke();

	}

	public void drawCubicCurve(Graphics graph)
	{
		// Start point
		Point2D p1 = new Point2D(100, 250);
		// Control point 1
		Point2D cp1 = new Point2D(300, 10);
		// Control point 2
		Point2D cp2 = new Point2D(300, 800);
		// End point
		Point2D p2 = new Point2D(500, 400);

		// Cubic curve bounds (Rectangle)
		Rectangle2D bounds = new CubicCurve2D(p1, cp1, cp2, p2).getBounds();
		graph.beginStroke("#CCCCCC");
		graph.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
		graph.endStroke();

		// P1 dot
		graph.beginStroke("#000000").beginFill("#000000").drawCircle(p1.x, p1.y, 5).endStroke().endFill();
		// CP1 dot
		graph.beginStroke("#00FF00").beginFill("#00FF00").drawCircle(cp1.x, cp1.y, 5).endStroke().endFill();
		// CP2 dot
		graph.beginStroke("#00FF00").beginFill("#00FF00").drawCircle(cp2.x, cp2.y, 5).endStroke().endFill();
		// P2 dot
		graph.beginStroke("#000000").beginFill("#000000").drawCircle(p2.x, p2.y, 5).endStroke().endFill();

		// Line connecting P1 / CP1 / CP2 / P2
		graph.beginStroke("#00FF00");
		graph.moveTo(p1.x, p1.y);
		graph.lineTo(cp1.x, cp1.y);
		graph.lineTo(cp2.x, cp2.y);
		graph.lineTo(p2.x, p2.y);
		graph.endStroke();

		// Cubic curve line
		graph.beginStroke("#FF0000");
		graph.moveTo(p1.x, p1.y);
		graph.bezierCurveTo(cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
		graph.endStroke();
	}
}
